package com.zx.mes.longsecurity.hyl.controller;

import com.zx.mes.hyl.common.controller.BaseController;
import com.zx.mes.hyl.common.pagemodel.*;
import com.zx.mes.longsecurity.hyl.domain.Tuser;
import com.zx.mes.longsecurity.hyl.pagemodel.Role;
import com.zx.mes.longsecurity.hyl.service.RoleServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.UUID;


/**
 * 角色控制器
 * 
 * @author 
 * 
 */
@Controller
@RequestMapping("/roleController")
public class RoleController extends BaseController {

	@Autowired
	private RoleServiceI roleService;

	/**
	 * 跳转到角色管理页面
	 * 
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager() {
		return "html/role/role-list";
	}

	/**
	 * 跳转到角色添加页面
	 * 
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		Role r = new Role();
		r.setId(UUID.randomUUID().toString());
		request.setAttribute("role", r);
		return "html/role/role-add";
	}


	@ResponseBody
	@RequestMapping("/list")
	public JsonResult<List> list(Role role, LayPageHelper ph ){
		JsonResult<List> jsonResult=new JsonResult<>();
		List<Role> roleList=roleService.findAll(role,ph);

		jsonResult.setIS_SUCCESS(true);
		jsonResult.setCode(0);
		jsonResult.setData(roleList);
		jsonResult.setCount((int) roleService.count(role));
		jsonResult.setMsg("");
		return jsonResult;
	}



	/**
	 * 添加角色
	 * 
	 * @return
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Json add(Role role) {

		Json j = new Json();

		try {
			roleService.add(role);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("添加失败！");
		}

		return j;
	}

	/**
	 * 跳转到角色修改页面
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage(HttpServletRequest request, String id) {
		Role r = roleService.get(id);
		request.setAttribute("role", r);
		return "html/role/role-edit";
	}

	/**
	 * 修改角色
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(Role role) {
		Json j = new Json();
		try {
			roleService.edit(role);
			j.setSuccess(true);
			j.setMsg("编辑成功！");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}

		return j;
	}


	/**
	 * 角色树
	 * 
	 * @return
	 */
	@RequestMapping("/allTree")
	@ResponseBody
	public List<Tree> allTree(String userId) {
//		Tuser tuser = (Tuser) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();

		Tuser tuser=new Tuser();
		tuser.setId(userId);
		return roleService.allTree(tuser);
	}

	@RequestMapping("/getAll")
	@ResponseBody
	public List<Tree> getAll() {
		return roleService.getAll();
	}

	/**
	 * 删除角色
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		roleService.delete(id);
		j.setMsg("删除成功！");
		j.setSuccess(true);
		return j;
	}

	/**
	 * 跳转到角色授权页面
	 * 
	 * @return
	 */
	@RequestMapping("/grantPage")
	public String grantPage(HttpServletRequest request, String id) {
		Role r = roleService.get(id);
		request.setAttribute("role", r);
		return "html/user/grant-list";
	}

	/**
	 * 选择父级页面
	 *
	 * @return
	 */
	@RequestMapping("/pidPage")
	public String pidPage(HttpServletRequest request, String id) {
		Role r = roleService.get(id);
		request.setAttribute("role", r);
		return "html/user/pid-list";
	}

	/**
	 * 授权
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("/grant")
	@ResponseBody
	public Json grant(Role role) {
		Json j = new Json();
		roleService.grant(role);
		j.setMsg("授权成功！");
		j.setSuccess(true);
		return j;
	}

}
